/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.shared;

import java.io.Serializable;

public class SerializableColor implements Serializable {

   public final static SerializableColor WHITE = new SerializableColor(255, 255, 255);
   public final static SerializableColor LIGHT_GRAY = new SerializableColor(192, 192, 192);
   public final static SerializableColor GRAY = new SerializableColor(128, 128, 128);
   public final static SerializableColor DARK_GRAY = new SerializableColor(64, 64, 64);
   public final static SerializableColor BLACK = new SerializableColor(0, 0, 0);
   public final static SerializableColor RED = new SerializableColor(255, 0, 0);
   public final static SerializableColor PINK = new SerializableColor(255, 175, 175);
   public final static SerializableColor ORANGE = new SerializableColor(255, 200, 0);
   public final static SerializableColor YELLOW = new SerializableColor(255, 255, 0);
   public final static SerializableColor GREEN = new SerializableColor(0, 255, 0);
   public final static SerializableColor MAGENTA = new SerializableColor(255, 0, 255);
   public final static SerializableColor CYAN = new SerializableColor(0, 255, 255);
   public final static SerializableColor BLUE = new SerializableColor(0, 0, 255);
   public static final SerializableColor NONE = new SerializableColor("");

   private int r, g, b;

   // only for special cases, like no color, or maybe named colors
   private String colorText = null;

   private SerializableColor() {
   }

   private SerializableColor(String colorText) {
      this.colorText = colorText;
   }

   public SerializableColor(int r, int g, int b) {
      this.r = r;
      this.g = g;
      this.b = b;
   }

   public int getRed() {
      return r;
   }

   public int getGreen() {
      return g;
   }

   public int getBlue() {
      return b;
   }

   public String getHexValue() {
      if (colorText != null) {
         return colorText;
      }

      return "#"
            + pad(Integer.toHexString(r))
            + pad(Integer.toHexString(g))
            + pad(Integer.toHexString(b));
   }

   private String pad(String in) {
      if (in.length() == 0) {
         return "00";
      }
      if (in.length() == 1) {
         return "0" + in;
      }
      return in;
   }

   public String toString() {
      if (colorText != null) {
         return colorText;
      }
      return "red=" + r + ", green=" + g + ", blue=" + b;
   }

   @Override
   public boolean equals(Object o) {
      if (this == o) {
         return true;
      }
      if (o == null || getClass() != o.getClass()) {
         return false;
      }

      SerializableColor that = (SerializableColor) o;

      if (b != that.b) {
         return false;
      }
      if (g != that.g) {
         return false;
      }
      if (r != that.r) {
         return false;
      }

      return true;
   }

   @Override
   public int hashCode() {
      int result = r;
      result = 31 * result + g;
      result = 31 * result + b;
      return result;
   }

   /**
    * Gets color from hex string
    * @param colorStr  the hex value
    * @return  the corresponding color
    */
   public static SerializableColor hex2Rgb(String colorStr) {
      return new SerializableColor(
            Integer.valueOf(colorStr.substring(1, 3), 16),
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16));
   }
}
